package net.dgg.zz.production.statistics.controller;


import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.common.constant.ScProductConstant;
import net.dgg.zz.common.service.CommonService;
import net.dgg.zz.common.utils.DateUtils;
import net.dgg.zz.common.utils.StringUtils;
import net.dgg.zz.common.utils.excel.ExcelUtils;
import net.dgg.zz.production.statistics.service.StatisticsService;
import net.dgg.zz.production.statistics.vo.OrderDetailVo;
import net.dgg.zz.production.statistics.vo.OrderRoyaltyVo;
import net.dgg.zz.production.vo.InprocessOrderVo;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
  * <p>@Title 资质生产统计Controller</p>
 * <p>@Description 资质生产统计相关</p>
 * <p>@Version 1.0.0 版本号</p>
 * <p>@author xiaoxiaoping</p>
 * <p>@date 2019年05月29日</p>
 * <p>Copyright © dgg group.All Rights Reserved. 版权信息</p>
 */
@Controller
@RequestMapping("/zzsc/statistics")
public class StatisticsController {
    @Autowired
    private StatisticsService statisticsService;
    @Resource
    private TranslateUtil translateUtil;
    @Resource
    private DesUtil desUtil;
    @Resource
    private CommonService commonService;

    @RequestMapping(value="/orderDeduction")
    public String orderDeductionPage(Model model){
        model.addAttribute("extractNode",statisticsService.extractNode());
        return "zzsc/statistics/orderDeduction";
    }

    @RequestMapping(value="/orderDetail")
    public String orderDetailPage(Model model){
        model.addAttribute("finishNode",statisticsService.queryfinshNode());
        model.addAttribute("product",statisticsService.queryProduct());
        model.addAttribute("flowType", commonService.queryTreeBook("FLOW_TYPE",1,2,null));
        model.addAttribute("orderStatus", commonService.queryTreeBook("ZZSC_PRODUCT_STATUS",1,2,null));
        return "zzsc/statistics/orderDetail";
    }


    @RequestMapping(value = "/deduction_list")
    @ResponseBody
    @NLog()
    public DataTableResponse queryDeductionList(@RequestParam Map params, HttpServletRequest request) {
        try {
            DataTableRequest r = DataTableUtils.getParam(request); //获取datatable参数
            params.put("start", r.getStart());//获取当前页
            params.put("limit", r.getLength());//获取每页条数
            if(!params.containsKey("nodeTimeStart")){
                params.put("date","all");
            }
            List<Map> listPage = statisticsService.getOderorderDeductionList(params);
            String[] transColum = {"flowType","status"};
            listPage = translateUtil.translateList(transColum, listPage);//翻译
            String[] hideColum = {"customerPhone"};//解密
            listPage = desUtil.decryptListMap(hideColum, listPage);
            CustomerNumberEncryptionUtil.encryptionList(hideColum, listPage);
            Integer count = Integer.parseInt(params.get("count").toString());
            return new DataTableResponse(r.getDraw(), count, count, listPage, "");

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @RequestMapping(value = "/detail_list")
    @ResponseBody
    @NLog()
    public DataTableResponse queryDetailList(@RequestParam Map params, HttpServletRequest request) {
        try {
            DataTableRequest r = DataTableUtils.getParam(request); //获取datatable参数
            params.put("start", r.getStart());//获取当前页
            params.put("limit", r.getLength());//获取每页条数
            Integer count =statisticsService.orderCount(params);
            List<Map> listPage = statisticsService.queryOrderDtail(params);
            String[] transColum = {"flowType","status"};
            listPage = translateUtil.translateList(transColum, listPage);//翻译
            String[] hideColum = {"customerPhone"};//解密
            listPage = desUtil.decryptListMap(hideColum, listPage);
            CustomerNumberEncryptionUtil.encryptionList(hideColum, listPage);
            return new DataTableResponse(r.getDraw(), count, count, listPage, "");

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @RequestMapping("/export_inprocess_product_order")
    public void exportSczzAbnormal(@RequestParam Map params, HttpServletResponse response) {
        try {
            String fileName = "订单提成数据_" + DateUtils.dateString(new Date(), "yyyyMMddHHmm") + ".xlsx";
            if(!params.containsKey("nodeTimeStart")){
                params.put("date","all");
            }
            List<OrderRoyaltyVo> list = statisticsService.exportOrderRoyalty(params);
            ExcelUtils.writeToFile(list, null, fileName, response, OrderRoyaltyVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/export_order_detail")
    public void exportOrderDetail(@RequestParam Map params, HttpServletResponse response) {
        try {
            String fileName = "接单明细数据_" + DateUtils.dateString(new Date(), "yyyyMMddHHmm") + ".xlsx";
            List<OrderDetailVo> list = statisticsService.exportOrderdetail(params);
            ExcelUtils.writeToFile(list, null, fileName, response, OrderDetailVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
